# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.selector import Selector
from fangchang.items import FangchangItem
from scrapy.http import Request
import re
import requests

class FsfcSpider(scrapy.Spider):
    name = "fsfc"
    allowed_domains = ["gov.cn"]
    all_houses_urls = []
    for x in range(1, 3):
        first_url = 'http://fsfc.fsjw.gov.cn/search/index.do?sw=&dn=&hx=&lx=&mj=&jg=&ys=0&od=-SORT;-STIME;-LASTMODIFYTIME;RELEVANCE&p=' + str(x)
        id_patten = re.compile(r'(?<=td><a class="cache" value=")\d+')
        all_id = re.findall(id_patten, requests.get(first_url).text)
        for every_id in all_id:
            house_url = 'http://fsfc.fsjw.gov.cn/hpms_project/roomView.jhtml?id=' + every_id
            all_houses_urls.append(house_url)
    start_urls = tuple(all_houses_urls)
    # rules =[
    #     Rule(LinkExtractor(allow=(r"http://fsfc.fsjw.gov.cn/search/index.do?sw=&dn=&hx=&lx=&mj=&jg=&ys=0&od=-SORT;-STIME;-LASTMODIFYTIME;RELEVANCE&p=\d+")),callback='parse_one')
        # Rule(LinkExtractor(allow=(r'http://fsfc.fsjw.gov.cn/hpms_project/roomView.jhtml?id=\d+')),callback='parse_one',follow=True))
    # ]
    def parse(self, response):
            item = FangchangItem()
            item['houses'] = response.xpath('//*[@id="content"]/div[2]/div[1]/div[2]/table/tr/td/strong/span/text()').extract_first()
            item['the_url'] = response.url
            item['district'] = response.xpath('//*[@id="content"]/div[2]/div[1]/div[2]/table/tr[3]/td[2]/text()').extract_first()
            # self.get_jsonurl(response, item)
            # self.get_buileding(response, item)
            patten = re.compile(r'(?<=<a id=["])\d+')
            all = re.findall(patten, response)
            for every in all:
                url = "http://fsfc.fsjw.gov.cn/hpms_project/room.jhtml?id=" + every
                req = requests.get("url").json()
                for i in range(len(req)):
                    thefloor = req[i].get('floorindex')
                    the_roomname = req[i].get('roomno')
                    item['floor'] = thefloor
                    item['room_name'] = the_roomname
                    item['json_url'] = url
                    return item
    # def get_jsonurl(self, response, item):
    #     patten = re.compile(r'(?<=<a id=["])\d+')
    #     all = re.findall(patten, response)
    #     for every in all:
    #         url = "http://fsfc.fsjw.gov.cn/hpms_project/room.jhtml?id=" + every
    #         req = requests.get("url").json()
    #         for i in range(len(req)):
    #             thefloor = req[i].get('floorindex')
    #             the_roomname = req[i].get('roomno')
    #             item['floor'] = thefloor
    #             item['room_name'] =the_roomname
    #             item['json_url'] = url
    # def get_buileding(self, response, item):
    #     all_buildings = response.css('.bot-a > a ')
    #     for build in all_buildings:
    #         item['building'] = build.extract_first()
    #
    #
